package cn.itcast.nems.studentrecord.web.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.itcast.ic.common.exception.v2.BizExceptionProducer;
import cn.itcast.nems.common.enumeration.NemsErrorBodyEnum;
import cn.itcast.nems.journal.dto.JournalRenewalDTO;
import cn.itcast.nems.journal.dto.JournalRenewalDiscountDTO;
import cn.itcast.nems.journal.dto.JournalRenewalLineDTO;
import cn.itcast.nems.journal.dto.JournalRenewalLineDiscountDTO;
import cn.itcast.nems.manager.util.CustomBeanUtil;
import cn.itcast.nems.studentrecord.web.vo.form.JournalRenewalEditVO;
import cn.itcast.nems.studentrecord.web.vo.form.JournalRenewalLineEditVO;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 升级凭单 controller 工具类
 * </p>
 *
 * @author zhouwei
 * @since 2023/12/8 11:14 AM
 **/
public class JournalRenewalWebUtil {
    private JournalRenewalWebUtil(){} ;

    /**
     * <p>
     * 检验保存草稿、提交请求参数
     * </p>
     *
     * @param editVO 请求参数
     * @author zhouwei
     * @since 2023/12/8 11:10 AM
     **/
    public static void  validateCreateRequestParam(JournalRenewalEditVO editVO) {
        Assert.notNull(editVO , BizExceptionProducer.produce(NemsErrorBodyEnum.PARAM_EMPTY ,"请求参数")) ;
        Assert.notNull(editVO.getTargetClazzCampusId(),BizExceptionProducer.produce(NemsErrorBodyEnum.PARAM_EMPTY,"校区ID"));
        Assert.notNull(editVO.getTargetClazzSubjectId(),BizExceptionProducer.produce(NemsErrorBodyEnum.PARAM_EMPTY,"学科ID"));
        Assert.notNull(editVO.getTargetClazzModelId(),BizExceptionProducer.produce(NemsErrorBodyEnum.PARAM_EMPTY,"授权模式ID"));
    }

    /**
     * <p>
     * 转换实体 JournalRenewalEditVO ->  List<JournalRenewalDTO>
     * </p>
     *
     * @param editVO  请求参数
     * @return JournalRenewalDTO 凭单DTO ，用于后续业务处理  
     * @author zhouwei
     * @since 2023/12/8 11:11 AM
     **/
    public static JournalRenewalDTO transferJournalRenewalDTO(JournalRenewalEditVO editVO) {
        JournalRenewalDTO renewalDTO  = CustomBeanUtil.copyProperties(editVO ,new JournalRenewalDTO()) ;
        if(CollectionUtil.isNotEmpty(editVO.getDiscountList()) ) {
            List<JournalRenewalDiscountDTO> discountList =  new ArrayList<>( );
            for (int i = 0; i < editVO.getDiscountList().size(); i++) {
                discountList.add(CustomBeanUtil.copyProperties(editVO.getDiscountList().get(i) ,new JournalRenewalDiscountDTO())) ;
            }
            renewalDTO.setDiscountList(discountList);
        }
        return renewalDTO ;
    }

    /**
     * <p>
     * 转换实体 List<JournalRenewalLineEditVO> ->  List<JournalRenewalLineDTO>
     * </p>
     * @param editVO 请求参数
     * @return List<JournalRenewalLineDTO> 凭单行数据
     * @author zhouwei
     * @since 2023/12/8 11:11 AM
     */
    public static List<JournalRenewalLineDTO> transferJournalRenewalLineDTO(JournalRenewalEditVO editVO){
        if(CollectionUtil.isEmpty(editVO.getLineList())) {
            return  null;
        }
        List<JournalRenewalLineDTO> lineList = new ArrayList<>( ) ;
        for (int i = 0; i < editVO.getLineList().size(); i++) {
            JournalRenewalLineEditVO lineVO = editVO.getLineList().get(i) ;
            JournalRenewalLineDTO lineDTO =  CustomBeanUtil.copyProperties( lineVO ,new JournalRenewalLineDTO()) ;
            if(CollectionUtil.isNotEmpty(lineVO.getDiscountList())) {
                List<JournalRenewalLineDiscountDTO> lineDiscountList =new ArrayList<>();
                for (int j = 0; j < lineVO.getDiscountList().size(); j++) {
                    JournalRenewalLineDiscountDTO lineDiscountDTO =  CustomBeanUtil.copyProperties(lineVO.getDiscountList().get(j) ,new JournalRenewalLineDiscountDTO()) ;
                    lineDiscountList.add(lineDiscountDTO) ;
                }
                lineDTO.setDiscountList(lineDiscountList);
            }
            if(CollectionUtil.isNotEmpty(lineVO.getDiscountCouponList())) {
                List<JournalRenewalLineDiscountDTO> lineDiscountList =new ArrayList<>();
                for (int j = 0; j < lineVO.getDiscountCouponList().size(); j++) {
                    JournalRenewalLineDiscountDTO lineDiscountDTO =  CustomBeanUtil.copyProperties(lineVO.getDiscountCouponList().get(j) ,new JournalRenewalLineDiscountDTO()) ;
                    lineDiscountList.add(lineDiscountDTO) ;
                }
                lineDTO.setDiscountCouponList(lineDiscountList);
            }
            lineList.add(lineDTO) ;
        }
        return  lineList ;

    }
}
